package com.patrol.inspection.Utils;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.UUID;

public class JsonUtils {
    private static final ObjectMapper mapper = new ObjectMapper();

    /**
     * 将jsonStr 的 NodeName节点的值修改为value
     * @param jsonStr
     * @param NodeName
     * @param value
     * @return
     */
    public static String updateJsonNode(String jsonStr,String NodeName,int value)  {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode nodeRoot = objectMapper.readTree(jsonStr);
            // nodeRoot为非叶子节点，本身即为ObjectNode类型
            ObjectNode objRoot = (ObjectNode)nodeRoot;
            // 增加新节点
            //  objRoot.put("add", "new node");
            // 修改节点
            objRoot.put(NodeName, value);
            String result = objectMapper.writeValueAsString(nodeRoot);
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return jsonStr;
    }

    /**
     * 返回jsonStr中nodeName的值
     * @param jsonStr
     * @param NodeName
     * @return
     */
    public static String getJsonNode(String jsonStr,String NodeName)  {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode nodeRoot = objectMapper.readTree(jsonStr);
            return  nodeRoot.get(NodeName)==null?"":nodeRoot.get(NodeName).asText();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 对象转json
     * @param obj 对象
     * @return json字符串
     */
    public static String convertObjToJSON(Object obj){
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * json 转对象
     * @param jsonString json字符串
     * @param valueType  对象类型
     * @return  对象
     * @param <T>
     */
    public static <T> T convertJsonToObject(String jsonString, Class<T> valueType) {
        try {
            return mapper.readValue(jsonString, valueType);
        } catch (Exception e) {
            // Handle or log the exception based on your requirements
            e.printStackTrace();
            return null;
        }
    }

    public static <T> T convertJsonToObject1(String jsonString, Class<T> valueType) {
        try {
            return mapper.readValue(jsonString, valueType);
        } catch (Exception e) {
            // Handle or log the exception based on your requirements
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解析简单的json 返回对应key 的值
     * {
     * 	"test": "123",
     * 	"aa": "bb"
     * }
     * @param jsonStr json字符串
     * @param key   key
     * @return  value
     */
    public static String parseSimpleJSON(String jsonStr, String key){
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode nodeRoot = objectMapper.readTree(jsonStr);
            return nodeRoot.get(key).asText();
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }

    }
}
